package com.zwy.customdialog

import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog


/**
 * 包括自定义EditText
 */
class CustomDialog3
    (context: Context?) : AlertDialog(context!!, R.style.CustomDialog) {
    private var listener: OnClickListener? = null
    private var tvCancel: TextView? = null
    private var tvConfirm: TextView? = null
    private var tvTitle: TextView? = null

    //    private TextView tvContent;
    private var editText: EditText? = null
    private var title: String? = null

    //    private String message;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.custom_dialog_layout_3)
        setCanceledOnTouchOutside(false)

        //设置对话框的显示位置
        setDialogStartPositon()

        //绑定控件
        initView()

        //初始化个组件的内容
        initText()

        //设置点击事件触发
        initEvent()
    }

    private fun setDialogStartPositon() {
        val lp = WindowManager.LayoutParams()
        val window = window
        if (window != null) {
            lp.copyFrom(window.attributes)
        }
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
        //设置起始位置
//        lp.x  = 100;
//        lp.y = 100;
        lp.gravity = Gravity.CENTER
        if (window != null) {
            window.attributes = lp
        }
    }

    private fun initEvent() {
        tvCancel!!.setOnClickListener { v: View? ->
            if (listener != null) listener!!.onCancelClick()
            dismiss()
        }
        tvConfirm!!.setOnClickListener { v: View? ->
            if (listener != null) listener!!.onConfirmClick(editText?.text.toString())
            dismiss()
        }
    }

    private fun initText() {
        if (title != null) tvTitle!!.text = title
        //        if (message != null) tvContent.setText(message);
        tvCancel!!.text = "取消"
        tvConfirm!!.text = "确认"
    }

    fun setTitle(title: String?): CustomDialog3 {
        this.title = title
        return this
    }

    //    public CustomDialog3 setMessage(String message) {
    //        this.message = message;
    //        return this;
    //    }
    private fun initView() {
        tvCancel = findViewById(R.id.tv_cancel)
        tvConfirm = findViewById(R.id.tv_confirm)
        tvTitle = findViewById(R.id.tv_title)
        //        tvContent = findViewById(R.id.tv_content);
        editText = findViewById(R.id.editText)
        showInputTips(editText!!)
    }

    //提供给外部使用的方法
    fun setOnClickListener(listener: OnClickListener?): CustomDialog3 {
        this.listener = listener
        return this
    }

    interface OnClickListener {
        fun onCancelClick()
        fun onConfirmClick(value: String)
    }

    private fun showInputTips(etText: EditText) {
        etText.isFocusable = true
        etText.isFocusableInTouchMode = true
        etText.requestFocus()
        //todo 遗留问题：点击EditText不弹出键盘--已解决
        //弹出对话框时需要点击输入框才能弹出软键盘
        this.window?.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)

    }
}
